<?php
/*
Extension Name: Shoutbox
Extension Url: http://vanillaforums.org/addon/506/shoutbox
Description: Adds a shoutbox to left panel and menu. It is linked with a special discussion thread. Requires jQuery add-on
Version: 0.5
Author: Jan 'anfit' Chimiak
Author Url: http://mmanir.net

Changes

Version 0.1 - fiat lux
Version 0.2 - settings page
Version 0.3 - css fix
Version 0.4 - fixes: deleted posts no longer show up in shoutbox
			- added 'Show shoutbox menu item' preference setting & tab position
Version 0.5 - fixed so that it really does post stuff from menu
 

*/

//configuration defaults
if (!array_key_exists('SHOUT_DISCUSSIONID', $Configuration)) {
	AddConfigurationSetting($Context, 'SHOUT_DISCUSSIONID', '0');
}
if (!array_key_exists('MAX_SHOUTS', $Configuration)) {
	AddConfigurationSetting($Context, 'MAX_SHOUTS', '7');
	}
if (!array_key_exists('SHOUTS_AGE_LIMIT', $Configuration)) {	
	AddConfigurationSetting($Context, 'SHOUTS_AGE_LIMIT', '25');
}
if (!array_key_exists('TAB_POSITION_SHOUTS', $Configuration)) {	
	AddConfigurationSetting($Context, 'TAB_POSITION_SHOUTS', '10');
}
if (!array_key_exists('SHOUTS_SHOW_MENU_ITEM', $Configuration)) {	
	AddConfigurationSetting($Context, 'SHOUTS_SHOW_MENU_ITEM', '1');
}
//definitions
$Context->SetDefinition('Shout', 'Shout');
$Context->SetDefinition('ShoutToday', 'Today');
$Context->SetDefinition('ShoutYesterday', 'Yesterday');
$Context->SetDefinition('ShoutHeader', 'Shoutbox');
$Context->SetDefinition('ShoutSubmit', 'Submit');
$Context->SetDefinition('ShoutTabname', 'Shout');
$Context->SetDefinition('SelectShoutboxThread', 'Select a thread for shoutbox');
$Context->SetDefinition('ShoutboxSettings', 'Shoutbox');
$Context->SetDefinition('ShoutboxDiscussion', 'Shoutbox discusion');
$Context->SetDefinition('ShoutboxDiscussionNotes', 'Select a discussion to be used as shoutbox. The select box shows only top 50 Discussions, if you want to use some older discussion make it sticky before trying to set it as shoutbox here.');
$Context->SetDefinition('ShoutboxThreads','Discussions');
$Context->SetDefinition('ShoutboxMaximumShouts', 'How many shouts can be visible.');
$Context->SetDefinition('ShoutboxAgeThreshold', 'Ignore shouts older than this many days.');
$Context->SetDefinition('ShoutboxShowMenuItem', 'Show shoutbox menu item linking to the shoutbox\' discussion.');
$Context->SetDefinition('ShoutboxShowMenuItemPosition', 'The position of the shoutbox in the menu. Note that its not a place in menu order but a positioning index.');

//path
$ShoutPath = dirname(__FILE__);

//libs
include($ShoutPath.'/library/Shoutbox.Class.ShoutBox.php');
include($ShoutPath.'/library/Shoutbox.Class.ShoutboxSettingsForm.php');


if (
	$Context->Session->UserID > 0 && // User must be logged in to see the shout tab
	array_key_exists('SHOUT_DISCUSSIONID', $Configuration) && //Shoutbox is defined 
	$Context->Configuration['SHOUT_DISCUSSIONID'] > 0 //Shoutbox's discussion has been chosen
) {

	$ShoutBox = $Context->ObjectFactory->NewContextObject($Context, 'ShoutBox');

	if($Context->SelfUrl == "index.php"){
		
		$Context->AddToDelegate('DiscussionManager', 'PreGetDiscussionList', 'Shoutbox_FilterDiscussionsList');
		function Shoutbox_FilterDiscussionsList(&$DiscussionManager) {
			global $Context;
			$DiscussionManager->DelegateParameters["SqlBuilder"]->AddWhere('t', 'DiscussionID', '', $Context->Configuration['SHOUT_DISCUSSIONID'], '!=');
		}
	}
	
	if(
		isset($Panel) && 
		!in_array($Context->SelfUrl, array("account.php","settings.php","search.php","extension.php"))
	){
	
		includeJQuery();		
		
		$Panel->AddString($ShoutBox->render($ShoutPath),false, $Position = '4', $ForcePosition = '4');
		$Head->AddStyleSheet('extensions/Shoutbox/style.css');
	
	}
	if (isset($Menu) && $Configuration['SHOUTS_SHOW_MENU_ITEM']) {
		$TabName = $Context->GetDefinition('Shout');
		$Menu->AddTab($TabName, $Context->GetDefinition('ShoutTabname'), GetUrl($Configuration, 'comments.php', '', '', '', '', 'DiscussionID='.$Context->Configuration['SHOUT_DISCUSSIONID'].'&page='.$ShoutBox->getShoutsLastPage()), '', $Configuration['TAB_POSITION_SHOUTS']); 
	}
}
		
if ($Context->SelfUrl == "settings.php" && $Context->Session->User->Permission('PERMISSION_MANAGE_EXTENSIONS')) {

	$ShoutboxSettingsForm = $Context->ObjectFactory->NewContextObject($Context, 'ShoutboxSettingsForm');
	$Page->AddRenderControl($ShoutboxSettingsForm, $Configuration['CONTROL_POSITION_BODY_ITEM'] + 1);

	$ExtensionOptions = $Context->GetDefinition('ExtensionOptions');
	$Panel->AddList($ExtensionOptions, 10);
	$Panel->AddListItem($ExtensionOptions, $Context->GetDefinition('ShoutboxSettings'), GetUrl($Context->Configuration, 'settings.php', '', '', '', '', 'PostBackAction=Shoutbox'));
}


?>